import MusicManager from "../../MusicManager";
import UpgradeSellDialogScene from "../../scene/dialog/UpgradeSellDialogScene";
import DestructibleFacility from "../facility/DestructibleFacility";
import { Global } from "../info/Global";
import PlayerManager from "../manager/PlayerManager";
import ControlledPlayer from "../player/ControlledPlayer";
import Player from "../player/Player";
import Dialog from "./Dialog";

const { ccclass, property } = cc._decorator;

@ccclass
export default class UpgradeSellDialog extends Dialog {

    @property(cc.Node)
    buy: cc.Node = null;

    @property(cc.Node)
    sell: cc.Node = null;

    @property(cc.Label)
    titleName: cc.Label = null;

    @property(cc.Label)
    explain: cc.Label = null;

    controllerPlayer: ControlledPlayer;

    upgradePrice: { diamond: number; oil: number; }
    sellPrice: { diamond: number; oil: number; }
    facility: DestructibleFacility;

    protected onEnable(): void {
        this.controllerPlayer = PlayerManager.instance.controllerPlayer;
        this.controllerPlayer.node.on(Player.EventType.更新晶石, this.refresh, this);
        this.controllerPlayer.node.on(Player.EventType.更新石油, this.refresh, this);
    }

    protected onDisable(): void {
        this.upgradePrice = null;
        this.facility = null;
        this.sellPrice = null;
        cc.isValid(this.controllerPlayer) && this.controllerPlayer.node.targetOff(this);
    }

    public init(facility: DestructibleFacility) {
        this.facility = facility;

        let facilityInfo = Global.getFacilityInfo<{
            名字: string, 说明: string, 每等级升级要求?: { diamond: number; oil: number; }[]
        }>(facility.facilityType);

        this.titleName.string = facilityInfo.名字;
        this.explain.string = facilityInfo.说明;
        if (facilityInfo.每等级升级要求) {
            this.upgradePrice = facilityInfo.每等级升级要求[facility.level];
        }

        this.sellPrice = { diamond: Math.ceil(facility.price.diamond / 2), oil: Math.ceil(facility.price.oil / 2) };
        this.buy.active = !!this.upgradePrice;
        if (this.buy.active) {
            let buyDiamond = cc.find("price/diamond", this.buy);
            let buyOil = cc.find("price/oil", this.buy);
            buyDiamond.active = !!this.upgradePrice.diamond
            buyDiamond && (buyDiamond.getComponentInChildren(cc.Label).string = this.upgradePrice.diamond.toString())
            buyOil.active = !!this.upgradePrice.oil
            buyOil && (buyOil.getComponentInChildren(cc.Label).string = this.upgradePrice.oil.toString())
            this.buy.getComponent(cc.Button).interactable = PlayerManager.instance.controllerPlayer.checkEnoughMoney(this.upgradePrice);
        }

        let sellDiamond = cc.find("price/diamond", this.sell);
        let sellOil = cc.find("price/oil", this.sell);
        sellDiamond.active = !!this.sellPrice.diamond;
        sellOil.active = !!this.sellPrice.oil;
        sellDiamond.active && (sellDiamond.getComponentInChildren(cc.Label).string = this.sellPrice.diamond.toString());
        sellOil.active && (sellOil.getComponentInChildren(cc.Label).string = this.sellPrice.oil.toString());
    }

    refresh() {
        this.init(this.facility);
        this.getComponent(UpgradeSellDialogScene).refresh();
    }

    onClick(event: cc.Event) {
        let player = PlayerManager.instance.controllerPlayer
        let name = event.target.name;
        MusicManager.instance.play(MusicManager.instance.btnMove);
        switch (name) {
            case "升级":
                this.facility.levelUp();
                this.refresh();
                break;
            case "卖出":
                player.addDiamondAndOil(this.sellPrice.diamond, this.sellPrice.oil, this.facility.node.position);
                this.facility.die(null);
                this.closeDialog();
                break;
        }
    }

    public closeDialog(): void {
        this.node.active = false;
    }
}
